// pages/xitongxiaoxi/xitongxiaoxi.js
const app = getApp()
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getXtUrl: app.data.url + '/api/my/getxitong',
    message_page:1,
    shuju: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    this.onReachBottom();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ message_page: 1 });
    this.setData({ shuju: [] });
    this.onShow();
    wx.stopPullDownRefresh({
      success: function () {
        console.log('返回了')
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var page = this.data.message_page;
      if (page == 0) {
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none'
        })
        return;
      }
      var that = this;
      var user = wx.getStorageSync('user');
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: that.data.getXtUrl,
        data: { token: user.token,page:page },
        method: 'post',
        success: function (res) {
          var data = res.data;
          console.log(data);
          if (data.code == 200) {
            var shuju = that.data.shuju;
            var array = data.data.data;
            // console.log(array);
            // return
            that.setData({ message_page: page + 1 });
            if (array.length < 10) {
              that.setData({ message_page: 0 });
            }
            // console.log(page);
            ea.each(array, function (i, v) {
              shuju.push(v);
            })
            that.setData({ shuju: shuju });
          } else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
          wx.hideLoading();
        }
      })

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})